package net.tngou.iask.action.api;

import java.util.HashMap;

import net.tngou.iask.action.BaseAction;
import net.tngou.iask.enums.LevelEnum;
import net.tngou.iask.pojo.Answer;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.pojo.Question_answer;
import net.tngou.iask.service.QuestionService;
import net.tngou.iask.util.StringUtil;

/**
 * 问题库
 * @author tngou@tngou.net
 *
 */
public class QuestionApi extends BaseAction {
	/**
	 * 添加问题
	 */
	@SuppressWarnings("serial")
	public void add() {		
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有添加权限");return;
		}
		Question bean = getAsk(Question.class);
		if(isEmpty(bean.getTitle(),bean.getName()))
		{
			run_false("请求的参数不正确！");return;
		}
		POJO pojo = bean.get(new HashMap<String, Object>(){{
			put("name", bean.getName());
			put("main", 0);
		}});
		if(pojo!=null)
		{
			run_false("已经存在该标签！");return;
		}
		if(user.getMain()!=0)
		{
			pojo = bean.get(new HashMap<String, Object>(){{
				put("name", bean.getName());
				put("main", user.getMain());
			}});
			if(pojo!=null)
			{
				run_false("已经存在该选项！");return;
			}
		}
		
		bean.setMain(user.getMain());
		
		long id = bean.save();
		QuestionService  service = new QuestionService();
//		String[] answers = request.getParameterValues("answers");//问题
		String[] filters = request.getParameterValues("filters");//过滤条件
		if(id>0){		
			service.update(id ,filters);
			saveLog(LevelEnum.system, "添加问题(id:"+id+"-"+bean.getName()+")");
			run_success(id+"");
		}
		else {
			run_false("添加失败！");
		}
	}
	
	/**
	 * 更新
	 */
	public void update() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Question bean = getAsk(Question.class);
		if(bean.getId()==0){
			run_false("请求的参数不正确！");return;
		}
		Question question = new Question().get(bean.getId());		
		if(question==null)
		{
			run_false("请求的参数不正确！");return;
		}
		if(StringUtil.isNotEmpty(bean.getName())&&!bean.getName().equals(question.getName()))
		{
			POJO pojo = bean.get(new HashMap<String, Object>(){{
				put("name", bean.getName());
			}});
			if(pojo!=null)
			{
				run_false("已经存在该标签！");return;
			}
			question.setName(bean.getName());
		}
		if(StringUtil.isNotEmpty(bean.getTitle()))question.setTitle(bean.getTitle());
		if(StringUtil.isNotEmpty(bean.getTag()))question.setTag(bean.getTag());
		if(StringUtil.isNotEmpty(request.getParameter("value")))question.setValue(bean.getValue());
		if(StringUtil.isNotEmpty(request.getParameter("type")))question.setType(bean.getType());
		long id = question.update();
		QuestionService  service = new QuestionService();
		String[] filters = request.getParameterValues("filters");//过滤条件
		if(id>0){
				service.update(id ,filters);

			saveLog(LevelEnum.system, "修改问题(id:"+id+"-"+bean.getName()+")");
			run_success("修改成功");
		}
		else {
			run_false("修改失败！");
		}
	}
	
	/**
	 * 更新答案顺序
	 */
	public void seq() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Question_answer bean = getAsk(Question_answer.class);
		if(bean.getId()==0||bean.getSeq()==0){
			run_false("请求的参数不正确！");return;
		}
		Question_answer question_answer=new Question_answer().get(bean.getId());
		if(question_answer==null){
			run_false("请求的参数不正确！");return;
		}
		question_answer.setSeq(bean.getSeq());
		long id = question_answer.update();
		if(id>0){
			saveLog(LevelEnum.system, "修改问题选项顺序(id:"+id+")");
			run_success("修改成功");
		}
		else {
			run_false("修改失败！");
		}
	}
	
	/**
	 * 添加答案
	 */
	public void answeradd() {
		
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有修改权限");return;
		}
		Question_answer bean = getAsk(Question_answer.class);
		if(bean.getQuestion()==0||bean.getAnswer()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Question_answer question_answer = new Question_answer().get("question="+bean.getQuestion()+" AND answer="+bean.getAnswer());
		if(question_answer!=null){
			run_success("添加成功");return;
		}
		Answer answer = new Answer().get(bean.getAnswer());
		Question question = new Question().get(bean.getQuestion());
		if(answer==null||question==null||question.getMain()!=user.getMain())
		{
			run_false("请求的参数不正确！");return;
		}
		int seq = bean.totalCount("question="+bean.getQuestion())+1;
		bean.setSeq(seq);
		long id = bean.save();
		if(id>0){
			saveLog(LevelEnum.system, "添加问题选项(id:"+id+")");
			question.setAnswercount(question.getAnswercount()+1);
			question.update();
			run_success("添加成功");
		}
		else {
			run_false("添加失败！");
		}
	}
	
	
	/**
	 * 删除问题选择
	 */
	public void answerdelete() {
		if(user==null){
			run_nologin();return;
		}
		if(user.getRole()!=1&&user.getRole()!=2&&user.getRole()!=3)
		{
			run_false("没有删除权限");return;
		}
		if(ask.getId()==0)
		{
			run_false("请求的参数不正确！");return;
		}
		Question_answer bean =new Question_answer().get(ask.getId());
		if(bean!=null)
		{
			Question question = new Question().get(bean.getQuestion());
			if(question==null||question.getMain()!=user.getMain())
			{
				run_false("没有删除权限");return;
			}else
			{
				boolean b = bean.delete();
				if(b){
					saveLog(LevelEnum.system, "删除问题中选项(id:"+question.getId()+")");
					question.setAnswercount(question.getAnswercount()-1);
					question.update();
					run_success("删除成功");
				}else
				{
					run_false("删除失败！");return;
				}
			}
		}else
		{
			run_false("请求的参数不正确！");return;
		}
	
	}
}
